package com.android.bookgarden.base;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.Toast;

import com.githang.statusbar.StatusBarCompat;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Created by zouqh on 2018/3/18.
 */

public abstract class BaseActivity extends AppCompatActivity {
    public static final int REQUEST_PREMISSIONS = 786542;
    private List<String> request_pres;
    private String request_type = "";



        @Override
        protected void onCreate( Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.setStates("#ffffff");
            setContentView(getLayoutId());
            ButterKnife.bind(this);
            this.initView();
        }

        protected abstract int getLayoutId();
        protected abstract void initView();




        public void startActivity(Class<?> cls) {
            startActivity(cls, null);
        }

        /**
         * 含有Bundle通过Class跳转界面
         **/
        public void startActivity(Class<?> cls, Bundle bundle) {
            Intent intent = new Intent(this, cls);
            if (bundle != null) {
                intent.putExtras(bundle);
            }
            startActivity(intent);
        }

        public void startActivityForResult(Class<?> cls, int requestCode) {
            startActivityForResult(cls, null, requestCode);
        }

        public void startActivityForResult(Class<?> cls, Bundle bundle, int requestCode) {
            Intent intent = new Intent(this, cls);
            if (bundle != null) {
                intent.putExtras(bundle);
            }
            startActivityForResult(intent, requestCode);
        }

        /**
         * 跳转界面并关闭当前界面
         *
         * @param clazz 目标Activity
         */
        protected void startThenKill(Class<?> clazz) {
            startThenKill(clazz, null);
        }

        /**
         * @param clazz  目标Activity
         * @param bundle 数据
         */
        protected void startThenKill(Class<?> clazz, Bundle bundle) {
            startActivity(clazz, bundle);
            finish();
        }

        protected void showToast(String msg){
            Toast.makeText(this,""+msg,Toast.LENGTH_SHORT).show();
        }



    public abstract void onRequestPermissions(String str, boolean z);

    public void checkSelfPermissions(String type, String[] pres) {
        this.request_type = type;
        List<String> need_reqquest_pres = new ArrayList();
        if (Build.VERSION.SDK_INT >= 23) {
            int n = pres.length;
            for (int i = 0; i < n; i++) {
                switch (checkSelfPermission(pres[i])) {
                    case -1:
                        need_reqquest_pres.add(pres[i]);
                        break;
                    default:
                        break;
                }
            }
            if (need_reqquest_pres.size() > 0) {
                String[] n_pres = new String[need_reqquest_pres.size()];
                need_reqquest_pres.toArray(n_pres);
                this.request_pres = need_reqquest_pres;
                requestPermissions(n_pres, REQUEST_PREMISSIONS);
                return;
            }
            onRequestPermissions(this.request_type, true);
            return;
        }
        onRequestPermissions(this.request_type, true);
    }

    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == REQUEST_PREMISSIONS) {
            int n = this.request_pres.size();
            if (permissions.length == n && grantResults.length == n) {
                int i = 0;
                while (i < n) {
                    String permission = permissions[i];
                    if (this.request_pres.contains(permission) && grantResults[i] == 0) {
                        this.request_pres.remove(permission);
                    }
                    i++;
                }
                if (this.request_pres.size() <= 0) {
                    this.request_pres = null;
                    onRequestPermissions(this.request_type, true);
                } else {
                    onRequestPermissions(this.request_type, false);
                }
            }
        }else{
            onRequestPermissions(this.request_type, false);
        }

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    public void tostShow(String msg){
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
    }

    /**
     * 设置状态栏颜色
     * @param color
     */
    public void setStates(String color) {
        if (TextUtils.isEmpty(color)) {
            return;
        }
        StatusBarCompat.setStatusBarColor(this, Color.parseColor(color), true);
    }
}
